﻿<Window x:Class="FlashNut.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:FlashNut"
    xmlns:ValidationRules="clr-namespace:ValidationRules"
    Title="Flash Nut" Height="400" Width="600">
    <Window.Resources>
        <ValidationRules:NotEmptyRule x:Key="NotEmptyRule"/>
        <ControlTemplate x:Key="TextBoxErrorTemplate">
            <StackPanel>
                <StackPanel Orientation="Horizontal">
                    <AdornedElementPlaceholder x:Name="Holder"/>
                </StackPanel>
                <Label Foreground="Red" Content="{Binding ElementName=Holder,Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"/>
            </StackPanel>
        </ControlTemplate>
        <Style x:Key="textBoxInError" TargetType="{x:Type TextBox}">
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="true">
                    <Setter Property="ToolTip"
        Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                        Path=(Validation.Errors)[0].ErrorContent}"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Window.CommandBindings>
        <CommandBinding Command="ApplicationCommands.New"
                  Executed="NewCmdExecuted"
                  CanExecute="NewCmdCanExecute"/>
        <CommandBinding Command="ApplicationCommands.Open"
                  Executed="OpenCmdExecuted"
                  CanExecute="OpenCmdCanExecute"/>
        <CommandBinding Command="ApplicationCommands.Save"
                  Executed="SaveCmdExecuted"
                  CanExecute="SaveCmdCanExecute"/>
        <CommandBinding Command="ApplicationCommands.Close"
                  Executed="CloseCmdExecuted"
                  CanExecute="CloseCmdCanExecute"/>
    </Window.CommandBindings>
    <DockPanel>
        <Menu DockPanel.Dock="Top" IsMainMenu="True">
            <MenuItem Header="_File">
                <MenuItem Header="New" Command="ApplicationCommands.New"/>
                <MenuItem Header="Open" Command="ApplicationCommands.Open"/>
                <MenuItem Header="Save" Command="ApplicationCommands.Save"/>
                <Separator/>
                <MenuItem Header="Close" Command="ApplicationCommands.Close"/>
            </MenuItem>
        </Menu>
        <TabControl Name="MainTabControl">
            <TabItem Header="Decks">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"></RowDefinition>
                        <RowDefinition Height="*"></RowDefinition>
                    </Grid.RowDefinitions>
                    <WrapPanel Grid.Row="0">
                        <TextBox Name="NewDeckTextBox" Width="150" Style="{StaticResource textBoxInError}">
                            <TextBox.Resources>
                                <TextBlock x:Key="tb" Text="NewDeck"/>
                            </TextBox.Resources>
                            <TextBox.Text>
                                <Binding Source="{StaticResource tb}" UpdateSourceTrigger="LostFocus" Path="Text">
                                    <Binding.ValidationRules>
                                        <ValidationRules:NotEmptyRule/>
                                    </Binding.ValidationRules>
                                </Binding>
                            </TextBox.Text>
                        </TextBox>
                        <Button Name="AddNewDeckButton" Content="Add New Deck" Click="AddNewDeckButton_Click"></Button>
                    </WrapPanel>
                    <ListView Grid.Row="1" Name="listViewDeck" ItemsSource="{Binding Deck}" >
                        <ListView.View>
                            <GridView>
                                <GridViewColumn Header="Name">
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <TextBox  Width="100" Text="{Binding Path=Name}" />
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>
                                <GridViewColumn>
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <Button Name="ReviewDeck" Content="Review" Tag="{Binding}" Click="ReviewDeck_Click"/>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>
                                <GridViewColumn>
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <Button Name="BuildDeck" Content="Build" Tag="{Binding}" Click="BuildDeck_Click"/>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>
                                <GridViewColumn>
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <Button Name="DeleteDeck" Content="Delete" Tag="{Binding}" Click="DeleteDeck_Click"/>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>
                            </GridView>
                        </ListView.View>
                    </ListView>
                </Grid>
            </TabItem>
            <TabItem Name="TabItemBuild" Header="Build" DataContext="{Binding ElementName=listViewDeck, Path=SelectedItem}">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"></RowDefinition>
                        <RowDefinition Height="Auto"></RowDefinition>
                        <RowDefinition Height="*"></RowDefinition>
                    </Grid.RowDefinitions>
                    <TextBlock Grid.Row="0" Text="{Binding Name}" HorizontalAlignment="Center"/>
                    <Grid Grid.Row="1">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"></RowDefinition>
                            <RowDefinition Height="Auto"></RowDefinition>
                            <RowDefinition Height="Auto" ></RowDefinition>
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width=".10*"></ColumnDefinition>
                            <ColumnDefinition Width=".60*"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <TextBlock Grid.Row="0" Grid.Column="0" Text="Question:"/>
                        <TextBox Grid.Row="0" Grid.Column="1" Name="QuestionTextBox" AcceptsReturn="True" MaxLines="10" Style="{StaticResource textBoxInError}">
                            <TextBox.Resources>
                                <TextBlock x:Key="tb" Text="Question"/>
                            </TextBox.Resources>
                            <TextBox.Text>
                                <Binding Source="{StaticResource tb}" UpdateSourceTrigger="LostFocus" Path="Text">
                                    <Binding.ValidationRules>
                                        <ValidationRules:NotEmptyRule/>
                                    </Binding.ValidationRules>
                                </Binding>
                            </TextBox.Text>
                        </TextBox>
                        <TextBlock Grid.Row="1" Grid.Column="0" Text="Answer:"/>
                        <TextBox Grid.Row="1" Grid.Column="1" Name="AnswerTextBox" AcceptsReturn="True" MaxLines="10" Style="{StaticResource textBoxInError}">
                            <TextBox.Resources>
                                <TextBlock x:Key="tb" Text="Answer"/>
                            </TextBox.Resources>
                            <TextBox.Text>
                                <Binding Source="{StaticResource tb}" UpdateSourceTrigger="LostFocus" Path="Text">
                                    <Binding.ValidationRules>
                                        <ValidationRules:NotEmptyRule/>
                                    </Binding.ValidationRules>
                                </Binding>
                            </TextBox.Text>
                        </TextBox>
                        <Button Grid.Row="2" Grid.ColumnSpan="2" Name="AddFlash" Tag="{Binding}" Content="Add" Click="AddFlash_Click"></Button>
                    </Grid>
                    <ListView Grid.Row="2" Name="listViewFlash" ItemsSource="{Binding Flash}" >
                        <ListView.View>
                            <GridView>
                                <GridViewColumn Header="Reviewed">
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <CheckBox IsChecked="{Binding Path=Review}"/>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>
                                <GridViewColumn>
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <Button Name="DeleteFlash" Content="Delete" Tag="{Binding}" Click="DeleteFlash_Click"/>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>
                                <GridViewColumn Header="Question">
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <TextBox  AcceptsReturn="True" MaxLines="10" Width="150" Text="{Binding Path=Question}" />
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>
                                <GridViewColumn Header="Answer">
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <TextBox  AcceptsReturn="True" MaxLines="10" Width="150" Text="{Binding Path=Answer}" />
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>
                            </GridView>
                        </ListView.View>
                    </ListView>
                </Grid>
            </TabItem>
            <TabItem Name="TabItemReview" Header="Review" DataContext="{Binding ElementName=listViewDeck, Path=SelectedItem}">
                <DockPanel>
                    <TextBlock DockPanel.Dock="Top" Grid.Row="0" Text="{Binding Name}" HorizontalAlignment="Center"/>
                    <Button DockPanel.Dock="Top" Name="ResetReview" Content="Reset Review" Click="ResetReview_Click"/>
                    <Button DockPanel.Dock="Top" Name="NextButton" Content="Next" Tag="{Binding ElementName=listViewReview, Path=SelectedItem}" Click="NextButton_Click"/>
                    <ListView Name="listViewReview" DockPanel.Dock="Bottom" ItemsSource="{Binding Flash}">
                        <ListView.ItemContainerStyle>
                            <Style TargetType="{x:Type ListViewItem}">
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding Path=Current}" Value="False">
                                        <Setter Property="FrameworkElement.Visibility" Value="Collapsed"/>
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding Path=Current}" Value="True">
                                        <Setter Property="FrameworkElement.Visibility" Value="Visible"/>
                                        <Setter Property="IsSelected" Value="true"/>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </ListView.ItemContainerStyle>
                        <ListView.View>
                            <GridView>
                                <GridView.ColumnHeaderContainerStyle>
                                    <Style>
                                        <Setter Property="FrameworkElement.Visibility" Value="Collapsed"/>
                                    </Style>
                                </GridView.ColumnHeaderContainerStyle>
                                <GridViewColumn>
                                    <GridViewColumn.CellTemplate>
                                        <DataTemplate>
                                            <DockPanel Height="200" Width="600" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="CornflowerBlue">
                                                <TextBlock Width="Auto" Height="Auto" >
                                                    <TextBlock.Resources>
                                                        <Style TargetType="{x:Type TextBlock}">
                                                            <Style.Triggers>
                                                                <Trigger Property="IsMouseOver" Value="false">
                                                                    <Setter Property="Text" Value="{Binding Path=Question}"/>
                                                                    <Setter Property="Background" Value="Aqua"/>
                                                                </Trigger>
                                                                <Trigger Property="IsMouseOver" Value="true">
                                                                    <Setter Property="Text" Value="{Binding Path=Answer}"/>
                                                                    <Setter Property="Background" Value="Crimson"/>
                                                                </Trigger>
                                                            </Style.Triggers>
                                                        </Style>
                                                    </TextBlock.Resources>
                                                </TextBlock>
                                            </DockPanel>
                                        </DataTemplate>
                                    </GridViewColumn.CellTemplate>
                                </GridViewColumn>
                            </GridView>
                        </ListView.View>
                    </ListView>
                </DockPanel>
            </TabItem>
        </TabControl>
    </DockPanel>
</Window>
